package com.example.gank.fragmet;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.core.base.BaseRcvAdapter;
import com.android.core.base.MVPBaseFragment;
import com.android.core.utils.ToastUtils;
import com.android.core.utils.log.Logger;
import com.example.gank.R;
import com.example.gank.activity.WebViewActivity;
import com.example.gank.adapter.AndroidGankAdapter;
import com.example.gank.bean.AndroidGankBean;
import com.example.gank.mvp.presenter.AndroidGankPresenter;
import com.example.gank.mvp.view.AndroidGankView;
import com.example.gank.widget.DividerListItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class AndroidGankFragment extends MVPBaseFragment<AndroidGankView, AndroidGankPresenter> implements AndroidGankView, SwipeRefreshLayout.OnRefreshListener, BaseRcvAdapter.OnItemClickListener {

    @BindView(R.id.rv_android_gank)
    RecyclerView rvAndroidGank;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    private int currPage = 1;
    private List<AndroidGankBean> androidGankBeens;
    private AndroidGankAdapter androidGankAdapter;

    public AndroidGankFragment() {
        // Required empty public constructor
    }


    public static AndroidGankFragment newInstance() {
        return new AndroidGankFragment();
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_android_gank;
    }

    @Override
    protected void onInitView(Bundle savedInstanceState) {
        Logger.i("abdc", "hahahah");

        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        androidGankBeens = new ArrayList<>();
        androidGankAdapter = new AndroidGankAdapter(mContext, R.layout.item_android_gank_rv, androidGankBeens);
        rvAndroidGank.setLayoutManager(new LinearLayoutManager(mContext));
        rvAndroidGank.addItemDecoration(new DividerListItemDecoration(mContext, LinearLayoutManager.VERTICAL));
        rvAndroidGank.setAdapter(androidGankAdapter);
        androidGankAdapter.setOnItemClickListener(this);
        rvAndroidGank.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                /**
                 * 1的值表示是否能向上滚动，false表示已经滚动到底部
                 * -1的值表示是否能向下滚动，false表示已经滚动到顶部
                 */
                if (!recyclerView.canScrollVertically(1)) {
                    currPage++;
                    loadNetData();
                }
            }
        });
        lazyLoad();
    }

    @Override
    protected AndroidGankPresenter createPresenter() {
        return new AndroidGankPresenter();
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        loadNetData();
    }

    private void loadNetData() {
        mPresenter.getAndroidGank(currPage);
    }

    @Override
    public void onRefresh() {
        currPage = 1;
        androidGankBeens.clear();
        loadNetData();
    }

    @Override
    public void onSucceed(List<AndroidGankBean> data) {
        mHasLoadedOnce = true;
        swipeRefreshLayout.setRefreshing(false);
        this.androidGankBeens.addAll(data);
        androidGankAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String err) {
        swipeRefreshLayout.setRefreshing(false);
        ToastUtils.showShort(err);
    }


    @Override
    public void onItemClick(View view, int position) {
        startActivity(new Intent(mContext, WebViewActivity.class).putExtra("url", androidGankBeens.get(position).getUrl()));
    }
}
